﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;

namespace StockMentor
{
    public partial class frmMain : Form
    {
        private SqlConnection conn;
        public frmMain(SqlConnection conn)
        {
            InitializeComponent();
            
            Rectangle rect = new Rectangle();
            
            rect = Screen.GetWorkingArea(this);
           
            this.Left = 0;
            
            this.Top = 0;
           
            this.Size = new Size(rect.Width, rect.Height);

            this.Text = "Stock MENTOR";

            this.conn = conn;
        }

        private void mnuItemFirma_Click(object sender, EventArgs e)
        {

            frmDateFirma formDateFirma = new frmDateFirma(conn);
            formDateFirma.MdiParent = this;
            //formDateFirma.Location = new Point(this.Left + (this.Width / 2 - formDateFirma.Width / 2), this.Top + (this.Height / 2 - formDateFirma.Height / 2));
            formDateFirma.StartPosition = FormStartPosition.CenterScreen;
            formDateFirma.Show();
            //formDateFirma.StartPosition = this.CenterToParent;
            
        }

        private void mnuItemReceptii_Click(object sender, EventArgs e)
        {
            frmReceptii formReceptii = new frmReceptii(conn);
            formReceptii.MdiParent = this;
            //formReceptii.Location = new Point(this.Left + (this.Width / 2 - formReceptii.Width / 2), this.Top + (this.Height / 2 - formReceptii.Height / 2));
            formReceptii.StartPosition = FormStartPosition.CenterScreen;
            formReceptii.Show();

        }

        private void mnuItemRecepEdit_Click(object sender, EventArgs e)
        {

            frmSituatii formSituatii = new frmSituatii(conn);
            formSituatii.MdiParent = this;
            //formRecepEdit.Location = new Point(this.Left + (this.Width / 2 - formRecepEdit.Width / 2), this.Top + (this.Height / 2 - formRecepEdit.Height / 2));
            formSituatii.StartPosition = FormStartPosition.CenterScreen;
            formSituatii.Show();
        }

        private void mnuItemFurnizor_Click(object sender, EventArgs e)
        {
            frmFurnizori formFurnizori = new frmFurnizori(conn);
            formFurnizori.MdiParent = this;
            //formFurnizori.Location = new Point(this.Left + (this.Width / 2 - formFurnizori.Width / 2), this.Top + (this.Height / 2 - formFurnizori.Height / 2));
            formFurnizori.StartPosition = FormStartPosition.CenterScreen;
            formFurnizori.Show();
        }

        private void mnuItemClienti_Click(object sender, EventArgs e)
        {
            frmClienti formClienti = new frmClienti(conn);
            formClienti.MdiParent = this;
            //formClienti.Location = new Point(this.Left + (this.Width / 2 - formClienti.Width / 2), this.Top + (this.Height / 2 - formClienti.Height / 2));
            formClienti.StartPosition = FormStartPosition.CenterScreen;
            formClienti.Show();

        }


        private void produseToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            frmProduse formProduse = new frmProduse(conn);
            formProduse.MdiParent = this;
            //formProduse.Location = new Point(this.Left + (this.Width / 2 - formProduse.Width / 2), this.Top + (this.Height / 2 - formProduse.Height / 2));
            formProduse.StartPosition = FormStartPosition.CenterScreen;
            formProduse.Show();
        }

        private void furnizoriToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmFurnizori formFurnizori = new frmFurnizori(conn);
            formFurnizori.MdiParent = this;
            //formFurnizori.Location = new Point(this.Left + (this.Width / 2 - formFurnizori.Width / 2), this.Top + (this.Height / 2 - formFurnizori.Height / 2));
            formFurnizori.StartPosition = FormStartPosition.CenterScreen;
            formFurnizori.Show();

        }

        private void clientiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmClienti formClienti = new frmClienti(conn);
            formClienti.MdiParent = this;
            //formClienti.Location = new Point(this.Left + (this.Width / 2 - formClienti.Width / 2), this.Top + (this.Height / 2 - formClienti.Height / 2));
            formClienti.StartPosition = FormStartPosition.CenterScreen;
            formClienti.Show();
        }

        private void receptiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmRecepEdit formRecepEdit = new frmRecepEdit(conn);
            formRecepEdit.MdiParent = this;
            //formRecepEdit.Location = new Point(this.Left + (this.Width / 2 - formRecepEdit.Width / 2), this.Top + (this.Height / 2 - formRecepEdit.Height / 2));
            formRecepEdit.StartPosition = FormStartPosition.CenterScreen;
            formRecepEdit.Show();
        }

        private void mnuItemFacturi_Click(object sender, EventArgs e)
        {
            frmFactura formFactura = new frmFactura(conn);
            formFactura.MdiParent = this;
            //formFactura.Location = new Point(this.Left + (this.Width / 2 - formFactura.Width / 2), this.Top + (this.Height / 2 - formFactura.Height / 2));
            formFactura.StartPosition = FormStartPosition.CenterScreen;
            formFactura.Show();

        }

        private void stocuriToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmStocuri formStocuri = new frmStocuri(conn);
            formStocuri.MdiParent = this;
            //formStocuri.Location = new Point(this.Left + (this.Width / 2 - formStocuri.Width / 2), this.Top + (this.Height / 2 - formStocuri.Height / 2));
            formStocuri.StartPosition = FormStartPosition.CenterScreen;
            formStocuri.Show();
        }

        private void facturiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmFacturaEdit formFactEdit = new frmFacturaEdit(conn);
            formFactEdit.MdiParent = this;
            //formFactEdit.Location = new Point(this.Left + (this.Width / 2 - formFactEdit.Width / 2), this.Top + (this.Height / 2 - formFactEdit.Height / 2));
            formFactEdit.StartPosition = FormStartPosition.CenterScreen;
            formFactEdit.Show();

        }

        private void produseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmSituatiiProduse formSitProd = new frmSituatiiProduse(conn);
            formSitProd.MdiParent = this;
            formSitProd.StartPosition = FormStartPosition.CenterScreen;
            formSitProd.Show();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            SqlCommand sqlComm = new SqlCommand("SELECT firma_cif FROM FIRMA", conn);
            SqlDataReader reader = sqlComm.ExecuteReader();

            if (!reader.HasRows)
            {
                reader.Close();
                reader.Dispose();
                reader = null;

                frmDateFirma formFirma = new frmDateFirma(conn);
                formFirma.MdiParent = this;
                formFirma.StartPosition = FormStartPosition.CenterScreen;
                formFirma.Show();

            }
            else
            {
                mnuItemFacturi.Visible = true;
                mnuItemReceptii.Visible = true;
                produseToolStripMenuItem1.Visible = true;
                produseToolStripMenuItem.Visible = true;
                stocuriToolStripMenuItem.Visible = true;
                receptiToolStripMenuItem.Visible = true;
                facturiToolStripMenuItem.Visible = true;
                furnizoriToolStripMenuItem.Visible = true;
                clientiToolStripMenuItem.Visible = true;
                mnuItemStocuriSit.Visible = true;
            
            }
            reader.Close();
            reader.Dispose();
            reader = null;
                
        }

        private void inchideToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void stripIesiri_Click(object sender, EventArgs e)
        {
            if (!verifyFirma())
            {
                MessageBox.Show("Pentru ca meniul sa fie actif, trebuie mai intai sa completati datele firmei !", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else mnuItemFacturi.Visible = true;
        }

        private void stripNomenclatoare_Click(object sender, EventArgs e)
        {
            if (!verifyFirma())
            {
                MessageBox.Show("Pentru ca meniul sa fie actif, trebuie mai intai sa completati datele firmei !", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                produseToolStripMenuItem1.Visible = true;
                clientiToolStripMenuItem.Visible = true;
                furnizoriToolStripMenuItem.Visible = true;
                facturiToolStripMenuItem.Visible = true;
                receptiToolStripMenuItem.Visible = true;
                stocuriToolStripMenuItem.Visible = true;

            }
        }

        private void stripRapoarte_Click(object sender, EventArgs e)
        {

            if (!verifyFirma())
            {
                MessageBox.Show("Pentru ca meniul sa fie actif, trebuie mai intai sa completati datele firmei !", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                produseToolStripMenuItem.Visible = true;
                mnuItemStocuriSit.Visible = true;
            }
        }

        private bool verifyFirma()
        {
            bool bReturn = false;

            DataSet dsFirma = new DataSet();
            SqlDataAdapter daFirma = new SqlDataAdapter("SELECT * FROM firma ", conn);
            daFirma.Fill(dsFirma, "firma");
            if (dsFirma.Tables["firma"].Rows.Count > 0) bReturn = true;
            
            dsFirma.Dispose();
            daFirma.Dispose();
            dsFirma = null;
            daFirma = null;

            return bReturn;

        }

        private void stripIntrari_MouseDown(object sender, MouseEventArgs e)
        {
            if (!verifyFirma())
            {
                MessageBox.Show("Pentru ca meniul sa fie actif, trebuie mai intai sa completati datele firmei !", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else mnuItemReceptii.Visible = true;
        }
    }
}
